package com.aiti.lulian.service.impl;

import cn.hutool.core.util.IdUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.ActivitySummaryDto;
import com.aiti.lulian.entity.*;
import com.aiti.lulian.mapper.ActivitySummaryMapper;
import com.aiti.lulian.mapper.ActivitySummaryPicMapper;
import com.aiti.lulian.mapper.ILuLianOfferAttachmentMapper;
import com.aiti.lulian.service.IActivitySummaryCooperateDetailService;
import com.aiti.lulian.service.IActivitySummaryPicService;
import com.aiti.lulian.service.IActivitySummaryService;
import com.aiti.lulian.service.IActivitySummaryWupiService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ActivitySummaryServiceImpl extends BaseSerivceImpl<ActivitySummaryMapper, ActivitySummary> implements IActivitySummaryService {

    private ActivitySummaryMapper activitySummaryMapper;

    private ActivitySummaryPicMapper activitySummaryPicMapper;

    private IActivitySummaryPicService activitySummaryPicService;

    private IActivitySummaryWupiService activitySummaryWupiService;
    @Autowired
    private IActivitySummaryCooperateDetailService activitySummaryCooperateDetailService;

    public ActivitySummaryServiceImpl(ActivitySummaryMapper activitySummaryMapper,
                                      ActivitySummaryPicMapper activitySummaryPicMapper,
                                      IActivitySummaryPicService activitySummaryPicService,
                                      IActivitySummaryWupiService activitySummaryWupiService) {
        this.activitySummaryMapper = activitySummaryMapper;
        this.activitySummaryPicMapper = activitySummaryPicMapper;
        this.activitySummaryWupiService = activitySummaryWupiService;
        this.activitySummaryPicService = activitySummaryPicService;
    }

    @Override
    public List<ActivitySummary> queryZjPage(Page<ActivitySummary> page, ActivitySummaryDto dto) {
        List<ActivitySummary> list = activitySummaryMapper.queryZjPage(page,dto);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Message uploadZj(ActivitySummary activitySummary) {

        String baseId = activitySummary.getBaseId();
        if(StringUtils.isBlank(baseId)){
            //新增操作
            String id = IdUtil.simpleUUID();
            activitySummary.setBaseId(id);
            activitySummary.setBaseCreateTime(new Date());

            activitySummaryMapper.insert(activitySummary);
            List<ActivitySummaryPic> picList = activitySummary.getPicList();
            if(picList != null && picList.size() > 0){
                List<ActivitySummaryPic> collect = picList.stream().map(k -> {
                    k.setBaseId(IdUtil.simpleUUID());
                    k.setPkId(id);
                    return k;
                }).collect(Collectors.toList());
                activitySummaryPicService.saveBatch(collect);
            }
            List<ActivitySummaryCooperateDetail> cooperateDetailList = activitySummary.getCooperateDetailList();
            if(!CollectionUtils.isEmpty(cooperateDetailList)){
                cooperateDetailList.forEach(f->{f.setSummaryId(id);f.setBaseId(IdUtil.fastUUID());});
                activitySummaryCooperateDetailService.saveBatch(cooperateDetailList);
            }
//            List<ActivitySummaryWupi> wupiList = activitySummary.getWupiList();
//            if(wupiList != null && wupiList.size() > 0){
//                List<ActivitySummaryWupi> collect = wupiList.stream().map(k -> {
//                    k.setBaseId(IdUtil.simpleUUID());
//                    k.setPkId(id);
//                    return k;
//                }).collect(Collectors.toList());
//                activitySummaryWupiService.saveBatch(collect);
//            }
        }else{
            activitySummary.setBaseUpdateTime(new Date());
            //更新时将状态重新置成未审核
            activitySummary.setStatus("0");
            activitySummary.setReject(null);
            activitySummaryMapper.updateById(activitySummary);

            //删除图片
            activitySummaryPicService.remove(new QueryWrapper<ActivitySummaryPic>().eq("pk_id",activitySummary.getBaseId()));

            List<ActivitySummaryPic> picList = activitySummary.getPicList();
            if(picList != null && picList.size() > 0){
                List<ActivitySummaryPic> collect = picList.stream().map(k -> {
                    k.setBaseId(IdUtil.simpleUUID());
                    k.setPkId(activitySummary.getBaseId());
                    return k;
                }).collect(Collectors.toList());
                activitySummaryPicService.saveBatch(collect);
            }
            activitySummaryCooperateDetailService.remove(new QueryWrapper<ActivitySummaryCooperateDetail>().eq("summary_id",activitySummary.getBaseId()));
            List<ActivitySummaryCooperateDetail> cooperateDetailList = activitySummary.getCooperateDetailList();
            if(!CollectionUtils.isEmpty(cooperateDetailList)){
                activitySummaryCooperateDetailService.updateBatchById(cooperateDetailList);
            }
//            activitySummaryWupiService.remove(new QueryWrapper<ActivitySummaryWupi>().eq("pk_id",activitySummary.getBaseId()));
//            List<ActivitySummaryWupi> wupiList = activitySummary.getWupiList();
//            if(wupiList != null && wupiList.size() > 0){
//                List<ActivitySummaryWupi> collect = wupiList.stream().map(k -> {
//                    k.setBaseId(IdUtil.simpleUUID());
//                    k.setPkId(activitySummary.getBaseId());
//                    return k;
//                }).collect(Collectors.toList());
//                activitySummaryWupiService.saveBatch(collect);
//            }
        }

        return Message.success();
    }

    @Override
    public Message queryShowData(String id) {
        ActivitySummary activitySummary = activitySummaryMapper.selectOne(new QueryWrapper<ActivitySummary>().eq("activity_id",id));
        if(activitySummary != null){
            List<ActivitySummaryWupi> wupiList = activitySummaryWupiService.list(new QueryWrapper<ActivitySummaryWupi>().eq("pk_id", activitySummary.getBaseId()));
            activitySummary.setWupiList(wupiList);

            List<ActivitySummaryPic> picList = activitySummaryPicService.list(new QueryWrapper<ActivitySummaryPic>().eq("pk_id", activitySummary.getBaseId()));
            activitySummary.setPicList(picList);

            QueryWrapper<ActivitySummaryCooperateDetail> wrapper = new QueryWrapper<>();
            wrapper.eq("summary_id",activitySummary.getBaseId());
            List<ActivitySummaryCooperateDetail> summaryCooperateDetailList = activitySummaryCooperateDetailService.list(wrapper);
            activitySummary.setCooperateDetailList(summaryCooperateDetailList);

        }
        return Message.success(activitySummary);
    }

    @Override
    public List<ActivitySummary>  getZjList(Page<ActivitySummary> page, ActivitySummaryDto dto) {
        List<ActivitySummary> zjList = activitySummaryMapper.getZjList(page, dto);
        return zjList;
    }

    @Override
    public void updateCheckStatus(ActivitySummary activitySummary) {
        UpdateWrapper<ActivitySummary> activitySummaryUpdateWrapper = new UpdateWrapper<ActivitySummary>();
        activitySummaryUpdateWrapper.eq("activity_id",activitySummary.getBaseId());
        activitySummaryUpdateWrapper.set("status",activitySummary.getStatus());
        activitySummaryUpdateWrapper.set(StringUtils.isNotBlank(activitySummary.getReject()),"reject",activitySummary.getReject());
        activitySummaryMapper.update(activitySummary,activitySummaryUpdateWrapper);
    }
}
